<?php
namespace Admin\Model;
use Think\Model;
class BrandModel extends Model {
	public $page; //当前页
	public $perpage; //每页显示
	public $brand_id; //ID
	public $post; //提交的信息 数组
	public $table='zjy_brand';
	public $ids; //IDs 数组，用于删除

	//获取列表
	public function get_brand_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		//$where='where a.member_state=1 and a.member_id=b.member_id ';
		if($this->keyword){
			$where.=" where `brand_name` LIKE '%".$this->keyword."%'";
		}

		$start=($page-1)*$perpage;
		$Model=new Model();


		$sql="SELECT brand_id,brand_name,brand_sort FROM `zjy_brand`  ".$where."order by brand_sort asc,brand_id desc limit $start,$perpage";
		$list=$Model->query($sql);

		return $list;
	}

	public function get_brand()
	{
		$brand_id=$this->brand_id;
		//echo $this->brand_id;exit();
		$Model=M("Brand"); // 实例化对象
		$list=$Model->field('brand_id,brand_name,brand_sort')->where('brand_id='.$brand_id)->find();
		return $list;

	}

	public function get_brand_total()
	{
		$Model=M("Brand");
		$count=$Model->count();
		return $count;
	}

	public function brand_add_post()
	{

		$Model = M("Brand"); // 实例化对象
		$data["brand_name"] = $this->post["brand_name"];
		$data["brand_sort"] = $this->post["brand_sort"];

		if (!$this->brandId) {
			$result = $Model->add($data);
		}else{
			$result = $Model->where('brand_id='.$this->brandId)->save($data);
		}

		if($result === false) {
			return false;
		}else{
			return true;
		}
	}

	public function update_brand_sort()
	{
		$ids=$this->ids;
		$sorts=$this->sorts;
		$Model=M("Brand");
		foreach($ids as $k=>$v){
			$data['brand_sort']=$sorts[$k];
			$condition['brand_id']=$v;
			$result=$Model->where($condition)->save($data);
			if($result === false) {
				return false;
			}
		}
		return true;
	}


	public function delete_brand()
	{
		$ids=$this->ids;
		$Model=M("Brand");
		foreach($ids as $v){
			$condition['brand_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}

}